#!/bin/sh
#
# Xstartup:	Do log on of the local or remote user by writing
#		utmp/wtmp/lastlog entries for the X session.
#
# Copyright (c) 1998-2000 SuSE GmbH Nuernberg, Germany.
# please send bugfixes or comments to feedback@suse.de.
#
# Author: Werner Fink,   <werner@suse.de>
#
  LIBDIR=/usr/X11R6/lib/X11
  ETCDIR=/etc/X11
  XDMDIR=${ETCDIR}/xdm
  BINDIR=/usr/X11R6/bin
xmessage=${BINDIR}/xmessage
 NOLOGIN=/etc/nologin

no_exit_on_failed_exec=1
type shopt &> /dev/null && shopt -s execfail
set +e     &> /dev/null

#
# The real user id
#
RUID=`id -ur $USER`

#
# If login is disabled, give an appropriate message
# and exit if normal user knock on.
#
if test -r $NOLOGIN ; then
   $xmessage -file $NOLOGIN -timeout 10 -default okay -center
   test "$RUID" != "0" && exit 1
fi

# 
# Find out if this is a local or remote connection
#
LOCATION=${DISPLAY%:*}
LINE=:${DISPLAY#*:}
if test -z "$LOCATION" ; then
    # local connection
    case "$LINE" in
	:0|:0.0) LOCATION=console   ;;
	*)       LOCATION=localhost ;;
    esac
else
    # TCP/IP connection (remote or local)
    LINE=${LINE}/${LOCATION%%.*}
fi

#
# Do not allow foreign root login if set in /etc/rc.config
#
while test "$RUID" = "0" ; do
    test "$LOCATION" = "console"       && break
    test "$LOCATION" = "localhost"     && break
    test "$LOCATION" = "`hostname -f`" && break
    test -s /etc/rc.config             && . /etc/rc.config
    test "$ROOT_LOGIN_REMOTE" = "yes"  && break
    $xmessage -timeout 10 -default okay -center "No foreign root login allowed"
    exit 1
done

#
# Make devices ready for the user.
#
case "$DISPLAY" in
    :0|:0.0)
	# Only for display :0  we have to set up the owner ship and
	# permissions of the /dev/xconsole FIFO and the current
	# virtual console /dev/tty0.

	${XDMDIR}/GiveDevices
	;;
    *)
esac

#
# Write utmp, wtmp, and lastlog entries.  Note that the `exec' is required
# to hold an exisiting pid in utmp during xsession.
#
exec ${BINDIR}/sessreg -l $LINE -h $LOCATION -a $USER
$xmessage -timeout 10 -default okay -center "$0: sessreg failed."
exit 1
